<!DOCTYPE html>
<html lang="zh-CN">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <meta name="robots" content="noodp" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge, chrome=1">
        <title>Keepalived_haproxy高可用 - 德国粗茶淡饭</title><meta name="Description" content="Keepalived_haproxy高可用"><meta property="og:title" content="Keepalived_haproxy高可用" />
<meta property="og:description" content="Keepalived_haproxy高可用" />
<meta property="og:type" content="article" />
<meta property="og:url" content="https://www.ctq6.cn/technology/linux/keepalived_haproxy%E9%AB%98%E5%8F%AF%E7%94%A8/" />
<meta property="og:image" content="https://www.ctq6.cn/logo.png"/>
<meta property="article:published_time" content="2021-02-16T19:05:31+08:00" />
<meta property="article:modified_time" content="2021-02-16T19:05:31+08:00" />
<meta name="twitter:card" content="summary_large_image"/>
<meta name="twitter:image" content="https://www.ctq6.cn/logo.png"/>

<meta name="twitter:title" content="Keepalived_haproxy高可用"/>
<meta name="twitter:description" content="Keepalived_haproxy高可用"/>
<meta name="application-name" content="LoveIt">
<meta name="apple-mobile-web-app-title" content="LoveIt"><meta name="theme-color" content="#ffffff"><meta name="msapplication-TileColor" content="#da532c"><link rel="shortcut icon" type="image/x-icon" href="/favicon.ico" />
        <link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
        <link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png"><link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png"><link rel="mask-icon" href="/safari-pinned-tab.svg" color="#5bbad5"><link rel="manifest" href="/site.webmanifest"><link rel="canonical" href="https://www.ctq6.cn/technology/linux/keepalived_haproxy%E9%AB%98%E5%8F%AF%E7%94%A8/" /><link rel="prev" href="https://www.ctq6.cn/technology/linux/git%E4%BD%BF%E7%94%A8/" /><link rel="next" href="https://www.ctq6.cn/technology/linux/shell%E7%BC%96%E7%A8%8B%E5%9F%BA%E7%A1%8001/" /><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/normalize.css@8.0.1/normalize.min.css"><link rel="stylesheet" href="/css/style.min.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.13.0/css/all.min.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/animate.css@3.7.2/animate.min.css"><script type="application/ld+json">
    {
        "@context": "http://schema.org",
        "@type": "BlogPosting",
        "headline": "Keepalived_haproxy高可用",
        "inLanguage": "zh-CN",
        "mainEntityOfPage": {
            "@type": "WebPage",
            "@id": "https:\/\/www.ctq6.cn\/technology\/linux\/keepalived_haproxy%E9%AB%98%E5%8F%AF%E7%94%A8\/"
        },"image": [{
                            "@type": "ImageObject",
                            "url": "https:\/\/www.ctq6.cn\/images\/Apple-Devices-Preview.png",
                            "width":  3200 ,
                            "height":  2048 
                        }],"genre": "technology","keywords": "Linux","wordcount":  1430 ,
        "url": "https:\/\/www.ctq6.cn\/technology\/linux\/keepalived_haproxy%E9%AB%98%E5%8F%AF%E7%94%A8\/","datePublished": "2021-02-16T19:05:31+08:00","dateModified": "2021-02-16T19:05:31+08:00","license": "This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License.","publisher": {
            "@type": "Organization",
            "name": "xxxx","logo": {
                    "@type": "ImageObject",
                    "url": "https:\/\/www.ctq6.cn\/images\/avatar.png",
                    "width":  528 ,
                    "height":  560 
                }},"author": {
                "@type": "Person",
                "name": "MikelPan"
            },"description": "Keepalived_haproxy高可用"
    }
    </script></head>
    <body header-desktop="fixed" header-mobile="auto"><script type="text/javascript">(window.localStorage && localStorage.getItem('theme') ? localStorage.getItem('theme') === 'dark' : ('auto' === 'auto' ? window.matchMedia('(prefers-color-scheme: dark)').matches : 'auto' === 'dark')) && document.body.setAttribute('theme', 'dark');</script>

        <div id="mask"></div><div class="wrapper"><header class="desktop" id="header-desktop">
    <div class="header-wrapper">
        <div class="header-title">
            <a href="/" title="德国粗茶淡饭"><span class="header-title-pre"><i class='far fa-kiss-wink-heart fa-fw'></i></span>德国粗茶淡饭</a>
        </div>
        <div class="menu">
            <div class="menu-inner"><a class="menu-item" href="/posts/"> 生活 </a><a class="menu-item" href="/technology/"> 技术 </a><a class="menu-item" href="/tags/"> 标签 </a><a class="menu-item" href="/categories/"> 分类 </a><a class="menu-item" href="/categories/documentation/"> 文档 </a><a class="menu-item" href="/about/"> 关于 </a><a class="menu-item" href="https://github.com/MikelPan/Cnblog.git" title="GitHub" rel="noopener noreffer" target="_blank"><i class='fab fa-github fa-fw'></i>  </a><span class="menu-item delimiter"></span><a href="javascript:void(0);" class="menu-item language" title="选择语言">简体中文<i class="fas fa-chevron-right fa-fw"></i>
                        <select class="language-select" id="language-select-desktop" onchange="location = this.value;"><option value="/technology/linux/keepalived_haproxy%E9%AB%98%E5%8F%AF%E7%94%A8/" selected>简体中文</option></select>
                    </a><span class="menu-item search" id="search-desktop">
                        <input type="text" placeholder="搜索文章标题或内容..." id="search-input-desktop">
                        <a href="javascript:void(0);" class="search-button search-toggle" id="search-toggle-desktop" title="搜索">
                            <i class="fas fa-search fa-fw"></i>
                        </a>
                        <a href="javascript:void(0);" class="search-button search-clear" id="search-clear-desktop" title="清空">
                            <i class="fas fa-times-circle fa-fw"></i>
                        </a>
                        <span class="search-button search-loading" id="search-loading-desktop">
                            <i class="fas fa-spinner fa-fw fa-spin"></i>
                        </span>
                    </span><a href="javascript:void(0);" class="menu-item theme-switch" title="切换主题">
                    <i class="fas fa-adjust fa-fw"></i>
                </a>
            </div>
        </div>
    </div>
</header><header class="mobile" id="header-mobile">
    <div class="header-container">
        <div class="header-wrapper">
            <div class="header-title">
                <a href="/" title="德国粗茶淡饭"><span class="header-title-pre"><i class='far fa-kiss-wink-heart fa-fw'></i></span>德国粗茶淡饭</a>
            </div>
            <div class="menu-toggle" id="menu-toggle-mobile">
                <span></span><span></span><span></span>
            </div>
        </div>
        <div class="menu" id="menu-mobile"><div class="search-wrapper">
                    <div class="search mobile" id="search-mobile">
                        <input type="text" placeholder="搜索文章标题或内容..." id="search-input-mobile">
                        <a href="javascript:void(0);" class="search-button search-toggle" id="search-toggle-mobile" title="搜索">
                            <i class="fas fa-search fa-fw"></i>
                        </a>
                        <a href="javascript:void(0);" class="search-button search-clear" id="search-clear-mobile" title="清空">
                            <i class="fas fa-times-circle fa-fw"></i>
                        </a>
                        <span class="search-button search-loading" id="search-loading-mobile">
                            <i class="fas fa-spinner fa-fw fa-spin"></i>
                        </span>
                    </div>
                    <a href="javascript:void(0);" class="search-cancel" id="search-cancel-mobile">
                        取消
                    </a>
                </div><a class="menu-item" href="/posts/" title="">生活</a><a class="menu-item" href="/technology/" title="">技术</a><a class="menu-item" href="/tags/" title="">标签</a><a class="menu-item" href="/categories/" title="">分类</a><a class="menu-item" href="/categories/documentation/" title="">文档</a><a class="menu-item" href="/about/" title="">关于</a><a class="menu-item" href="https://github.com/MikelPan/Cnblog.git" title="GitHub" rel="noopener noreffer" target="_blank"><i class='fab fa-github fa-fw'></i></a><a href="javascript:void(0);" class="menu-item theme-switch" title="切换主题">
                <i class="fas fa-adjust fa-fw"></i>
            </a><a href="javascript:void(0);" class="menu-item" title="选择语言">简体中文<i class="fas fa-chevron-right fa-fw"></i>
                    <select class="language-select" onchange="location = this.value;"><option value="/technology/linux/keepalived_haproxy%E9%AB%98%E5%8F%AF%E7%94%A8/" selected>简体中文</option></select>
                </a></div>
    </div>
</header>
<div class="search-dropdown desktop">
    <div id="search-dropdown-desktop"></div>
</div>
<div class="search-dropdown mobile">
    <div id="search-dropdown-mobile"></div>
</div>
<main class="main">
                <div class="container"><div class="page single special"><h1 class="single-title animated pulse faster">Keepalived_haproxy高可用</h1><div class="content" id="content"><h3 id="安装haproxy">安装haproxy</h3>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span><span class="lnt">23
</span><span class="lnt">24
</span><span class="lnt">25
</span><span class="lnt">26
</span><span class="lnt">27
</span><span class="lnt">28
</span><span class="lnt">29
</span><span class="lnt">30
</span><span class="lnt">31
</span><span class="lnt">32
</span><span class="lnt">33
</span><span class="lnt">34
</span><span class="lnt">35
</span><span class="lnt">36
</span><span class="lnt">37
</span><span class="lnt">38
</span><span class="lnt">39
</span><span class="lnt">40
</span><span class="lnt">41
</span><span class="lnt">42
</span><span class="lnt">43
</span><span class="lnt">44
</span><span class="lnt">45
</span><span class="lnt">46
</span><span class="lnt">47
</span><span class="lnt">48
</span><span class="lnt">49
</span><span class="lnt">50
</span><span class="lnt">51
</span><span class="lnt">52
</span><span class="lnt">53
</span><span class="lnt">54
</span><span class="lnt">55
</span><span class="lnt">56
</span><span class="lnt">57
</span><span class="lnt">58
</span><span class="lnt">59
</span><span class="lnt">60
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-bash" data-lang="bash"><span class="c1"># 安装</span>
yum install -y haproxy
<span class="c1"># 配置</span>
cat &gt; /etc/haproxy/haproxy.cfg <span class="s">&lt;&lt;- &#39;EOF&#39;
</span><span class="s">global
</span><span class="s">   log 127.0.0.1 local3 info         #在本机记录日志
</span><span class="s">   maxconn 65535                     #每个进程可用的最大连接数
</span><span class="s">   chroot /var/lib/haproxy         #haproxy 安装目录
</span><span class="s">   uid 188                            #运行haproxy的用户uid（cat /etc/passwd 查看，这里是nobody的uid）
</span><span class="s">   gid 188                           #运行haproxy的用户组id（cat /etc/passwd 查看，这里是nobody组id）
</span><span class="s">   daemon                            #以后台守护进程运行
</span><span class="s">   pidfile /var/run/haproxy.pid
</span><span class="s">   stats socket /var/lib/haproxy/stats
</span><span class="s">
</span><span class="s">defaults
</span><span class="s">   log global
</span><span class="s">   mode tcp                         #运行模式 tcp、 http、 health
</span><span class="s">   retries 3                         #三次连接失败，则判断服务不可用
</span><span class="s">   option redispatch                 #如果后端有服务器宕机，强制切换到正常服务器
</span><span class="s">   maxconn 65535                     #每个进程可用的最大连接数
</span><span class="s">   contimeout 5s
</span><span class="s">   # 客户端空闲超时时间为60秒，过了该时间，HA发起重连机制
</span><span class="s">   clitimeout 60s
</span><span class="s">   # 服务端连接超时时间为15秒，过了该时间，HA发起重连机制
</span><span class="s">   srvtimeout 15s
</span><span class="s">
</span><span class="s">
</span><span class="s">frontend http-in                     #自定义描述信息
</span><span class="s">   mode http                         #运行模式 tcp、 http、 health
</span><span class="s">   maxconn 65535                     #每个进程可用的最大连接数
</span><span class="s">   bind :80                          #监听 80 端口
</span><span class="s">   log global
</span><span class="s">   option httplog
</span><span class="s">   option httpclose                  #每次请求完毕后主动关闭 http 通道
</span><span class="s">   acl is_a hdr_beg(host) -i web1.mikel.top        #规则设置，-i 后面是要访问的域名
</span><span class="s">   acl is_b hdr_beg(host) -i web2.mikel.top        #如果多个域名，就写多个规则，一规则对应一个域名；即后面有多个域名，就写 is_c、 is-d….，这个名字可以随意起。但要与下面的use_backend 对应
</span><span class="s">   use_backend web-server if is_a    #如果访问 is_a 设置的域名，就负载均衡到下面backend 设置的对应 web-server 上。web-server所负载的域名要都部署到下面的web01和web02上。如果是不同的域名部署到不同的机器上，就定义不同的web-server。
</span><span class="s">   use_backend web-server if is_b
</span><span class="s">
</span><span class="s">backend web-server
</span><span class="s">   mode http
</span><span class="s">   balance roundrobin                #设置负载均衡模式，source 保存 session 值，roundrobin 轮询模式
</span><span class="s">   cookie SERVERID insert indirect nocache
</span><span class="s">   option httpclose
</span><span class="s">   option forwardfor
</span><span class="s">   server web01 172.16.0.6:5000 weight 1 cookie 3 check inter 2000 rise 2 fall 5
</span><span class="s">   server web02 172.16.0.9:5000 weight 1 cookie 4 check inter 2000 rise 2 fall 5
</span><span class="s">
</span><span class="s">listen status_page
</span><span class="s">   mode http
</span><span class="s">   bind :8000
</span><span class="s">   stats admin if TRUE
</span><span class="s">   stats uri /haproxy                #统计页面 URL 路径
</span><span class="s">   stats refresh 30s                 #统计页面自动刷新时间
</span><span class="s">   #stats realm itlihao\ welcome        #统计页面输入密码框提示信息
</span><span class="s">   stats auth admin:dxInCtFianKtL]36   #统计页面用户名和密码
</span><span class="s">   stats hide-version                 #隐藏统计页面上 HAProxy 版本信息
</span><span class="s">EOF</span>
<span class="c1"># 启动</span>
yum install -y haproxy
</code></pre></td></tr></table>
</div>
</div><h3 id="安装keepalived">安装keepalived</h3>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">  1
</span><span class="lnt">  2
</span><span class="lnt">  3
</span><span class="lnt">  4
</span><span class="lnt">  5
</span><span class="lnt">  6
</span><span class="lnt">  7
</span><span class="lnt">  8
</span><span class="lnt">  9
</span><span class="lnt"> 10
</span><span class="lnt"> 11
</span><span class="lnt"> 12
</span><span class="lnt"> 13
</span><span class="lnt"> 14
</span><span class="lnt"> 15
</span><span class="lnt"> 16
</span><span class="lnt"> 17
</span><span class="lnt"> 18
</span><span class="lnt"> 19
</span><span class="lnt"> 20
</span><span class="lnt"> 21
</span><span class="lnt"> 22
</span><span class="lnt"> 23
</span><span class="lnt"> 24
</span><span class="lnt"> 25
</span><span class="lnt"> 26
</span><span class="lnt"> 27
</span><span class="lnt"> 28
</span><span class="lnt"> 29
</span><span class="lnt"> 30
</span><span class="lnt"> 31
</span><span class="lnt"> 32
</span><span class="lnt"> 33
</span><span class="lnt"> 34
</span><span class="lnt"> 35
</span><span class="lnt"> 36
</span><span class="lnt"> 37
</span><span class="lnt"> 38
</span><span class="lnt"> 39
</span><span class="lnt"> 40
</span><span class="lnt"> 41
</span><span class="lnt"> 42
</span><span class="lnt"> 43
</span><span class="lnt"> 44
</span><span class="lnt"> 45
</span><span class="lnt"> 46
</span><span class="lnt"> 47
</span><span class="lnt"> 48
</span><span class="lnt"> 49
</span><span class="lnt"> 50
</span><span class="lnt"> 51
</span><span class="lnt"> 52
</span><span class="lnt"> 53
</span><span class="lnt"> 54
</span><span class="lnt"> 55
</span><span class="lnt"> 56
</span><span class="lnt"> 57
</span><span class="lnt"> 58
</span><span class="lnt"> 59
</span><span class="lnt"> 60
</span><span class="lnt"> 61
</span><span class="lnt"> 62
</span><span class="lnt"> 63
</span><span class="lnt"> 64
</span><span class="lnt"> 65
</span><span class="lnt"> 66
</span><span class="lnt"> 67
</span><span class="lnt"> 68
</span><span class="lnt"> 69
</span><span class="lnt"> 70
</span><span class="lnt"> 71
</span><span class="lnt"> 72
</span><span class="lnt"> 73
</span><span class="lnt"> 74
</span><span class="lnt"> 75
</span><span class="lnt"> 76
</span><span class="lnt"> 77
</span><span class="lnt"> 78
</span><span class="lnt"> 79
</span><span class="lnt"> 80
</span><span class="lnt"> 81
</span><span class="lnt"> 82
</span><span class="lnt"> 83
</span><span class="lnt"> 84
</span><span class="lnt"> 85
</span><span class="lnt"> 86
</span><span class="lnt"> 87
</span><span class="lnt"> 88
</span><span class="lnt"> 89
</span><span class="lnt"> 90
</span><span class="lnt"> 91
</span><span class="lnt"> 92
</span><span class="lnt"> 93
</span><span class="lnt"> 94
</span><span class="lnt"> 95
</span><span class="lnt"> 96
</span><span class="lnt"> 97
</span><span class="lnt"> 98
</span><span class="lnt"> 99
</span><span class="lnt">100
</span><span class="lnt">101
</span><span class="lnt">102
</span><span class="lnt">103
</span><span class="lnt">104
</span><span class="lnt">105
</span><span class="lnt">106
</span><span class="lnt">107
</span><span class="lnt">108
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-bash" data-lang="bash"><span class="c1"># 配置文件</span>
cat &gt; /etc/keepalived/keepalived.conf <span class="s">&lt;&lt;- &#39;EOF&#39;
</span><span class="s">! Configuration File for keepalived
</span><span class="s">global_defs {
</span><span class="s">  notification_email {
</span><span class="s">    root@localhost
</span><span class="s">    }
</span><span class="s">
</span><span class="s">notification_email_from keepalived@localhost
</span><span class="s">smtp_server 127.0.0.1
</span><span class="s">smtp_connect_timeout 30
</span><span class="s">router_id HAproxy237
</span><span class="s">script_user root
</span><span class="s">enable_script_security
</span><span class="s">}
</span><span class="s">
</span><span class="s">vrrp_script chk_haproxy {                                   #HAproxy 服务监控脚本
</span><span class="s">  script &#34;/etc/keepalived/check_haproxy.sh&#34;
</span><span class="s">  interval 2
</span><span class="s">  weight 2
</span><span class="s">}
</span><span class="s">
</span><span class="s">vrrp_instance VI_1 {
</span><span class="s">  state BACKUP
</span><span class="s">  nopreempt
</span><span class="s">  interface eth0
</span><span class="s">  virtual_router_id 51
</span><span class="s">  priority 100
</span><span class="s">  unicast_src_ip 172.16.0.6  # 配置单播的源地址
</span><span class="s">  unicast_peer { 
</span><span class="s">      172.16.0.9       #配置单播的目标地址
</span><span class="s">  } 
</span><span class="s">  advert_int 1
</span><span class="s">  authentication {
</span><span class="s">    auth_type PASS
</span><span class="s">    auth_pass 1111
</span><span class="s">}
</span><span class="s">  track_script {
</span><span class="s">    chk_haproxy
</span><span class="s">}
</span><span class="s">virtual_ipaddress {
</span><span class="s">    172.16.0.10
</span><span class="s">}
</span><span class="s">!notify_master &#34;/etc/keepalived/clean_arp.sh 182.148.15.239&#34;
</span><span class="s">}
</span><span class="s">EOF</span>
<span class="c1">## BACKUP</span>
cat &gt; /etc/keepalived/keepalived.conf <span class="s">&lt;&lt;- &#39;EOF&#39;
</span><span class="s">! Configuration File for keepalived
</span><span class="s">global_defs {
</span><span class="s">  notification_email {
</span><span class="s">    root@localhost
</span><span class="s">    }
</span><span class="s">
</span><span class="s">notification_email_from keepalived@localhost
</span><span class="s">smtp_server 127.0.0.1
</span><span class="s">smtp_connect_timeout 30
</span><span class="s">router_id HAproxy237
</span><span class="s">script_user root
</span><span class="s">enable_script_security
</span><span class="s">}
</span><span class="s">
</span><span class="s">vrrp_script chk_haproxy {                                   #HAproxy 服务监控脚本
</span><span class="s">  script &#34;/etc/keepalived/check_haproxy.sh&#34;
</span><span class="s">  interval 2
</span><span class="s">  weight 2
</span><span class="s">}
</span><span class="s">
</span><span class="s">vrrp_instance VI_1 {
</span><span class="s">  state BACKUP
</span><span class="s">  nopreempt
</span><span class="s">  interface eth0
</span><span class="s">  virtual_router_id 51
</span><span class="s">  priority 100
</span><span class="s">  unicast_src_ip 172.16.0.9  # 配置单播的源地址
</span><span class="s">  unicast_peer { 
</span><span class="s">      172.16.0.6       #配置单播的目标地址
</span><span class="s">  } 
</span><span class="s">  advert_int 1
</span><span class="s">  authentication {
</span><span class="s">    auth_type PASS
</span><span class="s">    auth_pass 1111
</span><span class="s">}
</span><span class="s">  track_script {
</span><span class="s">    chk_haproxy
</span><span class="s">}
</span><span class="s">virtual_ipaddress {
</span><span class="s">    172.16.0.10
</span><span class="s">}
</span><span class="s">!notify_master &#34;/etc/keepalived/clean_arp.sh 182.148.15.239&#34;
</span><span class="s">}
</span><span class="s">EOF</span>
<span class="c1"># 检测脚本</span>
cat &gt; /etc/keepalived/check_nginx.sh <span class="s">&lt;&lt;- &#39;EOF&#39;
</span><span class="s">#!/bin/bash
</span><span class="s">A=`ps -C haproxy --no-header | wc -l`
</span><span class="s">if [ $A -eq 0 ]
</span><span class="s">then
</span><span class="s">    systemctl restart haproxy
</span><span class="s">    sleep 3
</span><span class="s">    if [ `ps -C haproxy --no-header | wc -l ` -eq 0 ]
</span><span class="s">    then
</span><span class="s">        systemctl restart stop
</span><span class="s">    fi
</span><span class="s">fi
</span><span class="s">EOF</span>
<span class="c1"># 启动</span>
yum install -y keepalived
</code></pre></td></tr></table>
</div>
</div><h3 id="安装docker-环境">安装docker 环境</h3>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-bash" data-lang="bash">curl -s https://gitee.com/YunFeiGuoJi/Cnblog/raw/master/shell/Scripts/docker_install.sh <span class="p">|</span>sh
</code></pre></td></tr></table>
</div>
</div><h3 id="创建后端服务">创建后端服务</h3>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span><span class="lnt">23
</span><span class="lnt">24
</span><span class="lnt">25
</span><span class="lnt">26
</span><span class="lnt">27
</span><span class="lnt">28
</span><span class="lnt">29
</span><span class="lnt">30
</span><span class="lnt">31
</span><span class="lnt">32
</span><span class="lnt">33
</span><span class="lnt">34
</span><span class="lnt">35
</span><span class="lnt">36
</span><span class="lnt">37
</span><span class="lnt">38
</span><span class="lnt">39
</span><span class="lnt">40
</span><span class="lnt">41
</span><span class="lnt">42
</span><span class="lnt">43
</span><span class="lnt">44
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-bash" data-lang="bash"><span class="c1"># 编写flsk脚本</span>
cat &gt; app.py <span class="s">&lt;&lt;- &#39;EOF&#39;
</span><span class="s">from flask import Flask
</span><span class="s">app = Flask(__name__)
</span><span class="s">
</span><span class="s">@app.route(&#39;/&#39;)
</span><span class="s">def hello_world():
</span><span class="s">    return &#39;Hello World!&#39;
</span><span class="s">
</span><span class="s">if __name__ == &#39;__main__&#39;:
</span><span class="s">    app.run(host=&#39;0.0.0.0&#39;)
</span><span class="s">EOF</span>
<span class="c1"># 构建镜像</span>
cat &gt; build_image.sh <span class="s">&lt;&lt;- &#39;EOF&#39;
</span><span class="s">#!/usr/bin/env bash
</span><span class="s">mkdir -pv /usr/local/src/Dockerfile/tools
</span><span class="s">cp /root/app.py /usr/local/src/Dockerfile/tools
</span><span class="s"># 创建dockerfile
</span><span class="s">cat &gt; /usr/local/src/Dockerfile/Dockerfile-web1 &lt;&lt;- &#39;eof&#39;
</span><span class="s">FROM python:3.8.3-alpine3.12
</span><span class="s">WORKDIR /apps
</span><span class="s">COPY tools /apps/tools
</span><span class="s">RUN ls /apps/tools \
</span><span class="s">    &amp;&amp; pip3 install pipreqs \
</span><span class="s">    &amp;&amp; pipreqs tools/ --encoding=utf8 --force \
</span><span class="s">    &amp;&amp; pip3 install -r tools/requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
</span><span class="s">ENTRYPOINT [&#34;python3&#34;,&#34;/apps/tools/app.py&#34;]
</span><span class="s">CMD [&#34;runserver&#34;]
</span><span class="s">eof
</span><span class="s"># build 镜像
</span><span class="s">cd /usr/local/src/Dockerfile
</span><span class="s">docker build -t dockerplyx/service:web1-v1.0 -f Dockerfile-web1 --no-cache .
</span><span class="s">EOF</span>
<span class="c1"># 创建docker 启动脚本</span>
cat &gt; web2_restart.sh <span class="s">&lt;&lt;- &#39;EOF&#39;
</span><span class="s">#!/usr/bin/env bash
</span><span class="s">docker stop web2
</span><span class="s">docker rm web2
</span><span class="s">docker run --name web2 \
</span><span class="s">    --restart=always \
</span><span class="s">    -p 5000:5000  \
</span><span class="s">    -v /root/app.py:/apps/tools/app.py \
</span><span class="s">    -d dockerplyx/service:web1-v1.0
</span><span class="s">EOF</span>
</code></pre></td></tr></table>
</div>
</div></div><div id="comments"></div></div></div>
            </main><footer class="footer">
        <div class="footer-container"><div class="footer-line"><i class="far fa-copyright fa-fw"></i><span itemprop="copyrightYear">2019 - 2021</span><span class="author" itemprop="copyrightHolder">&nbsp;<a href="/" target="_blank">mikel pan</a></span>&nbsp;|&nbsp;<span class="license"><a rel="license external nofollow noopener noreffer" href="https://creativecommons.org/licenses/by-nc/4.0/" target="_blank">CC BY-NC 4.0</a></span><span class="icp-splitter">&nbsp;|&nbsp;</span><br class="icp-br"/>
                    <span class="icp"><a href="https://beian.miit.gov.cn/" target="_blank">粤ICP备2021047442号</a></span></div>
        </div>
    </footer></div>

        <div id="fixed-buttons"><a href="#" id="back-to-top" class="fixed-button" title="回到顶部">
                <i class="fas fa-arrow-up fa-fw"></i>
            </a><a href="#" id="view-comments" class="fixed-button" title="查看评论">
                <i class="fas fa-comment fa-fw"></i>
            </a>
        </div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/lightgallery.js@1.2.0/dist/css/lightgallery.min.css"><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/smooth-scroll@16.1.3/dist/smooth-scroll.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/autocomplete.js@0.37.1/dist/autocomplete.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/algoliasearch@4.2.0/dist/algoliasearch-lite.umd.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/lazysizes@5.2.2/lazysizes.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/lightgallery.js@1.2.0/dist/js/lightgallery.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/lg-thumbnail.js@1.2.0/dist/lg-thumbnail.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/lg-zoom.js@1.2.0/dist/lg-zoom.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/clipboard@2.0.6/dist/clipboard.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/sharer.js@0.4.0/sharer.min.js"></script><script type="text/javascript">window.config={"code":{"copyTitle":"复制到剪贴板","maxShownLines":10},"comment":{},"lightGallery":{"actualSize":false,"exThumbImage":"data-thumbnail","hideBarsDelay":2000,"selector":".lightgallery","speed":400,"thumbContHeight":80,"thumbWidth":80,"thumbnail":true},"search":{"algoliaAppID":"REQJX89W85","algoliaIndex":"index.zh-cn","algoliaSearchKey":"63fa048de9b35627f46672e95abc14df","highlightTag":"em","maxResultLength":10,"noResultsFound":"没有找到结果","snippetLength":50,"type":"algolia"}};</script><script type="text/javascript" src="/js/theme.min.js"></script></body>
</html>
